<?php
/**
 * 新手福利管理
 */
namespace Admin\Controller;
use Think\Controller;
use Think\Log;
class PBeginnerWelfareController extends Controller
{
    protected $return;
   
    public function __construct(){
        header("Access-Control-Allow-Origin: *");
        $this->return = array('errcode'=>0, 'errmsg'=>'ok');
        $admin = session('rush_admin');
        if (!$admin) {
        }
    }

    public function index()
    {
        $data['name'] = I('post.name', '', 'trim');
        $data['start'] = I('post.start', '', 'trim');
        $data['end'] = I('post.end', '', 'trim');
        
        $data['page'] = I('post.page', 1, 'intval');
        $data['page_size'] = I('page_size', 10, 'intval');
        Log::write(var_export($data,true), 'data');
        $where = array();
        if ($data['name'] != '') {
            $where['name'] = array('LIKE', '%'.$data['name'].'%');
        }
        // if ($data['status'] != -1) {
        //     $map['status'] = $data['status'];
        // }
        //活动创建时间开始区间
        if ($data['start']) {
            $where['start_time'][] = array('egt', strtotime($data['start']));
        }
        //活动创建时间结束区间
        if ($data['end']) {
            $where['end_time'][] = array('elt', strtotime($data['end']));
        }
        $where['is_deleted'] = 0;
        $total = M("PBeginnerWelfare")->where($where)->count();
        $page = pagination($total, $data['page_size'] ,$data['page']);
        if ($total) {
            $list = M("PBeginnerWelfare")->where($where)->order('id desc')
                  ->limit($page['offset'], $page['limit'])
                  ->select();
            foreach ($list as $k => $v) {
                $list[$k]['add_time'] = date('Y-m-d H:i', $v['add_time']);
                $list[$k]['start_time'] = date('Y-m-d H:i', $v['start_time']);
                $list[$k]['end_time'] = date('Y-m-d H:i', $v['end_time']);
            }
            $res =  array('data'=>$list, 'page'=>$page);
        }else{
            $res = array('data'=>array(), 'page'=>$page);
        }
        return $this->ajaxReturn(array_merge($this->return, $res));
    }

    public function updateStatus()
    {
        $id = I('post.id', 0, 'intval');
        $status = I('post.status');
        if (!in_array($status, array(0, 1))) {
            return $this->ajaxReturn(array('errcode'=>10001, 'errmsg'=>'状态错误'));
        }
        $res = M("PActivity")->where(array('id'=>$id))->setField('status', $status);
        if ($res !== false) {
            return $this->ajaxReturn($this->return);
        }else{
            return $this->ajaxReturn(array('errcode'=>10002, 'errmsg'=>'更新状态失败'));
        }

    }

    public function add()
    {
        $data['name'] = I('post.name', '', 'trim');
        $data['url'] = I('post.url', '', 'trim');
        $data['link'] = I('post.link', '', 'trim');
        $data['start_time'] = I('post.start_time', '', 'trim');
        $data['end_time'] = I('post.end_time', '', 'trim');

        if (empty($data['name'])) {
            return $this->ajaxReturn(array('errcode'=>10001, 'errmsg'=>'活动名称不能为空'));
        }
        if (empty($data['url'])) {
            return $this->ajaxReturn(array('errcode'=>10003, 'errmsg'=>'活动图片不能为空'));
        }
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        if ($data['start_time'] >= $data['end_time']) {
            return $this->ajaxReturn(array('errcode'=>10008, 'errmsg'=>'兑换时间错误'));
        }
        $map['name'] = $data['name'];
        $map['is_deleted'] = 0;
        $count = M("PBeginnerWelfare")->where($map)->count();
        if ($count) {
            return $this->ajaxReturn(array('errcode'=>20001, 'errmsg'=>'新手福利名称已存在'));
        }
        $data['add_time'] = time();
        $res = M("PBeginnerWelfare")->add($data);
        if ($res) {
            return $this->ajaxReturn(array('errcode'=>0, 'errmsg'=>'新增新手福利成功'));
        }else{
            return $this->ajaxReturn(array('errcode'=>50000, 'errmsg'=>'新增新手福利失败'));
        }
    }
        
    public function edit()
    {
        $id = I('post.id', 0, 'intval');
        $res = M("PBeginnerWelfare")->find($id);
        if ($res) {
            $res['start_time'] = date('Y-m-d H:i:s', $res['start_time']);
            $res['end_time'] = date('Y-m-d H:i:s', $res['end_time']);
            return $this->ajaxReturn(array('errcode'=>0, 'errmsg'=>'ok', 'data'=>$res));
        }else{
            return $this->ajaxReturn(array('errcode'=>50000, 'errmsg'=>'查找不到对应的新手福利数据'));
        }
    }

    public function update()
    {
        $data['name'] = I('post.name', '', 'trim');
        $data['url'] = I('post.url', '', 'trim');
        $data['link'] = I('post.link', '', 'trim');
        // Log::write(var_export($data, true), 'data');
        $data['id'] = I('post.id', 0, 'intval');
        $data['start_time'] = I('post.start_time', '', 'trim');
        $data['end_time'] = I('post.end_time', '', 'trim');
        if (empty($data['name'])) {
            return $this->ajaxReturn(array('errcode'=>10001, 'errmsg'=>'新手福利名称不能为空'));
        }
        if (empty($data['url'])) {
            return $this->ajaxReturn(array('errcode'=>10003, 'errmsg'=>'新手福利图片不能为空'));
        }

        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        if ($data['start_time'] >= $data['end_time']) {
            return $this->ajaxReturn(array('errcode'=>10008, 'errmsg'=>'兑换福利时间错误'));
        }
        $map['id'] = $data['id'];
        // Log::write(var_export($map, true), "11111");
        $act = M("PBeginnerWelfare")->where($map)->find();
        // Log::write(M("PBeginnerWelfare")->getLastSql(), "sql");
        if (!$act) {
            return $this->ajaxReturn(array('errcode'=>20001,'errmsg'=>'要修改的活动不存在'));
        }
        unset($map);
        $map['name'] = $data['name'];
        $map['is_deleted'] = 0;
        $act = M("PBeginnerWelfare")->where($map)->find();
        if ($act and $act['id'] != $data['id']) {
            return $this->ajaxReturn(array('errcode'=>20001, 'errmsg'=>'活动名称已存在'));
        }
        $res = M("PBeginnerWelfare")->save($data);
        if ($res !== false) {
            return $this->ajaxReturn(array('errcode'=>0, 'errmsg'=>'修改活动成功'));
        }else{
            return $this->ajaxReturn(array('errcode'=>50000, 'errmsg'=>'修改活动失败'));
        }
    }

    public function del()
    {
        $id = I('post.id', 0, 'intval');
        $data['is_deleted'] = 1;
        // Log::write($id);
        $res = M("PBeginnerWelfare")->where(array('id'=>$id))->save($data);
        // Log::write(M("PBeginnerWelfare")->getLastSql());
        if ($res) {
            $this->ajaxReturn($this->return);
        }else{
            $this->ajaxReturn(array('errcode'=>50000, 'errmsg'=>'删除失败'));
        }
    }

}